package com.stadium.service.impl;

import java.util.*;

import com.stadium.common.utils.SqlMapper;
import com.stadium.common.utils.UserUtils;
import com.stadium.dto.RRecruitPhotoDto;
import com.stadium.dto.RecruitPlayersDto;
import com.stadium.entity.RRecruitPhoto;
import com.stadium.entity.RecruitPlayers;
import com.stadium.mapper.GameTeamMP;
import com.stadium.mapper.RecruitPlayersMP;
import com.stadium.service.RRecruitPhotoSV;
import com.stadium.service.RecruitPlayersSV;
import com.stadium.vo.RRecruitPhotoVo;
import com.stadium.vo.RecruitPlayersVo;
import com.stadium.vo.searchVo.RRecruitPhotoSearchVo;
import com.stadium.vo.searchVo.RecruitPlayersSearchVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class RecruitPlayersSVI implements RecruitPlayersSV {
    @Autowired
    private RecruitPlayersMP mapper;
    @Autowired
    private SqlMapper sqlMapper;
    @Autowired
    private RRecruitPhotoSV rRecruitPhotoSV;

    @Override
    public List<RecruitPlayers> queryBySQL(String sql, Map<String, Object> map) {
        return sqlMapper.selectList(sql, map, RecruitPlayers.class);
    }

    @Override
    public void save(RecruitPlayersVo recruitPlayersVo) {
        RecruitPlayers recruitPlayers = new RecruitPlayers();
        BeanUtils.copyProperties(recruitPlayersVo, recruitPlayers);
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        recruitPlayers.setCreateUser(UserUtils.getUserId());
        recruitPlayers.setUpdateUser(UserUtils.getUserId());
        recruitPlayers.setId(id);
        recruitPlayers.setReleaseTime(new Date());
        mapper.save(recruitPlayers);
        if (recruitPlayersVo.getPhotoVoList() != null && recruitPlayersVo.getPhotoVoList().size() > 0) {
            List<RRecruitPhoto> rJoinFiles = new ArrayList<>();
            for (RRecruitPhotoVo rJoinFileVo : recruitPlayersVo.getPhotoVoList()) {
                RRecruitPhoto rJoinFile = new RRecruitPhoto();
                rJoinFile.setRecruitId(recruitPlayers.getId());
                rJoinFile.setPath(rJoinFileVo.getPath());
                rJoinFile.setSort(rJoinFileVo.getSort());
                rJoinFile.setCreateUser(UserUtils.getUserId());
                rJoinFile.setUpdateUser(UserUtils.getUserId());
                rJoinFiles.add(rJoinFile);
            }
            rRecruitPhotoSV.saveBatch(rJoinFiles);
        }
    }

    @Override
    public void update(RecruitPlayersVo recruitPlayersVo) {
        RecruitPlayers recruitPlayers = new RecruitPlayers();
        BeanUtils.copyProperties(recruitPlayersVo, recruitPlayers);
        recruitPlayers.setUpdateUser(UserUtils.getUserId());
        mapper.update(recruitPlayers);
    }

    @Override
    public void saveBatch(List<RecruitPlayers> list) {
        mapper.saveBatch(list);
    }

    @Override
    public void delete(String id) {
        mapper.delete(id);
    }

    @Override
    public void deleteBatch(String[] ids) {
        mapper.deleteBatch(ids);
    }

    @Override
    public List<RecruitPlayersDto> query(RecruitPlayersSearchVo searchVo) {
        List<RecruitPlayersDto> query = mapper.query(searchVo);
        query.stream().forEach(recruitPlayersDto -> {
            RRecruitPhotoSearchVo rRecruitPhotoSearchVo = new RRecruitPhotoSearchVo();
            rRecruitPhotoSearchVo.setRecruitId(recruitPlayersDto.getId());
            List<RRecruitPhotoDto> rRecruitPhotoDtos = rRecruitPhotoSV.query(rRecruitPhotoSearchVo);
            if ( rRecruitPhotoDtos != null) {
                recruitPlayersDto.setPhotos(rRecruitPhotoDtos);
            }else {
                recruitPlayersDto.setPhotos(new ArrayList<>(0));
            }
        });
        return query;

    }

    @Override
    public RecruitPlayersDto get(String id) {
        RecruitPlayersDto recruitPlayersDto = mapper.get(id);
        RRecruitPhotoSearchVo rRecruitPhotoSearchVo = new RRecruitPhotoSearchVo();
        rRecruitPhotoSearchVo.setRecruitId(recruitPlayersDto.getId());
        List<RRecruitPhotoDto> query = rRecruitPhotoSV.query(rRecruitPhotoSearchVo);
        if ( query != null) {
            recruitPlayersDto.setPhotos(query);
        }else {
            recruitPlayersDto.setPhotos(new ArrayList<>(0));
        }
        return recruitPlayersDto;
    }

    @Override
    public int getCount(RecruitPlayersSearchVo searchVo) {
        return mapper.getCount(searchVo);
    }

}
